<?php

namespace App\Admin\Controllers;

use App\Models\Book;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;

class BookController extends AdminController
{
    protected $title = '单词书';

    public function grid()
    {
        $grid = new Grid(new Book());
        $grid->model()->orderByDesc('sort');

        $grid->column('id', 'ID');
        $grid->column('key', '唯一标识');
        $grid->column('cover', '封面')->image('', 100);
        $grid->column('name', '名称');
        $grid->column('full_name', '全称');
        $grid->column('word_num', '单词总数')->display(function ($num) {
            return '<a href="'.admin_route('word.index').'?book_id='.$this->id.'">'.$num.'</a>';
        });
        $grid->column('publisher', '版本');
        $grid->column('origin', '來源')->limit(8);
        $grid->column('organization', '来源机构');

        $grid->filter(function (Grid\Filter $filter) {
            $filter->panel()->expand();
            $filter->like('name', '名称')->width(2);
            $filter->equal('category_id', '分类')->select(Book::$category)->width(2);
            $filter->scope('enable', '启用')->where('is_enable', 1);
            $filter->scope('disable', '禁用')->where('is_enable', 0);
        });

        return $grid;
    }

    public function form()
    {
        $form = new Form(new Book());

        $form->image('cover', '封面');
        $form->select('category_id', '级别')->options(Book::$category);
        $form->text('key', '唯一标识');
        $form->text('name', '名称');
        $form->text('full_name', '全称');
        $form->textarea('introduce', '介绍');
        $form->text('publisher', '版本');
        $form->text('origin', '來源');
        $form->text('organization', '来源机构');

        return $form;
    }

}
